package com.ouc.controller;

import com.ouc.common.config.OUCAjaxResult;
import com.ouc.common.utils.file.FileUtils;
import com.ouc.dataMange.domain.SysDataShip;
import com.ouc.dataMange.domain.SysDataShipPath;
import com.ouc.dataMange.service.ISysDataSetService;
import com.ouc.dataMange.service.ISysDataShipPathService;
import com.ouc.dataMange.service.ISysDataShipService;
import com.ouc.dataMange.service.ISysMemoryDeviceService;
import com.ouc.domain.*;
import com.ouc.service.NavigationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.util.ArrayList;
import java.util.List;

@Controller
@RestController
@RequestMapping("/navigation")
public class NavigationController {

  @Autowired
  private ISysDataSetService sysDataSetService;

  @Autowired
  private ISysMemoryDeviceService sysMemoryDeviceService;

  @Autowired
  private NavigationService navigationService;

  @Autowired
  private ISysDataShipService dataShipService;

  @Autowired
  private ISysDataShipPathService dataShipPathService;

  @ResponseBody
  @RequestMapping("/creatPre")
  public OUCAjaxResult creatPre(@RequestParam(value = "dataSetId", required = false, defaultValue = "11") Integer dataSetId,
                                @RequestParam(value = "memoryId", required = false, defaultValue = "14") Integer memoryId,
                                PreFile preFile) {
    try {
      String filePath = sysMemoryDeviceService.selectSysMemoryDeviceById(memoryId).getSysMemoryDevice() +
          sysDataSetService.selectSysDataSetById(dataSetId).getDataSetDevice() + "\\pre.in";
      preFile.setPreFilePath(filePath);
      navigationService.creatPre(preFile);
      return OUCAjaxResult.ok("pre文件创建成功，路径为" + filePath);
    } catch (Exception e) {
      return OUCAjaxResult.error(e.toString());
    }
  }

  @ResponseBody
  @RequestMapping("/runPre")
  public OUCAjaxResult runPre(@RequestParam(value = "dataSetId", required = false, defaultValue = "11") Integer dataSetId,
                              @RequestParam(value = "memoryId", required = false, defaultValue = "14") Integer memoryId) {
    try {
      String preDir = sysMemoryDeviceService.selectSysMemoryDeviceById(memoryId).getSysMemoryDevice() +
          sysDataSetService.selectSysDataSetById(dataSetId).getDataSetDevice();
      String runPre = navigationService.runPre(preDir);
      if (runPre.contains("预处理完成")) {
        return OUCAjaxResult.ok(runPre);
      } else {
        return OUCAjaxResult.error("error");
      }
    } catch (Exception e) {
      return OUCAjaxResult.error(e.toString());
    }
  }

  @ResponseBody
  @RequestMapping("/getPreResult")
  public OUCAjaxResult getPreResult(@RequestParam(value = "dataSetId", required = false, defaultValue = "11") Integer dataSetId,
                                    @RequestParam(value = "memoryId", required = false, defaultValue = "14") Integer memoryId,
                                    @RequestParam(value = "jobName") String jobName) {
    try {
      PreFile preFile = new PreFile();
      preFile.setJobName(jobName);
      preFile.setPreFilePath(sysMemoryDeviceService.selectSysMemoryDeviceById(memoryId).getSysMemoryDevice() +
          sysDataSetService.selectSysDataSetById(dataSetId).getDataSetDevice());
      return OUCAjaxResult.ok(navigationService.getPreResult(preFile));
    } catch (Exception e) {
      return OUCAjaxResult.error(e.toString());
    }
  }

  @ResponseBody
  @RequestMapping("/creatMain")
  public OUCAjaxResult creatMain(@RequestParam(value = "dataSetId", required = false, defaultValue = "11") Integer dataSetId,
                                 @RequestParam(value = "memoryId", required = false, defaultValue = "14") Integer memoryId,
                                 MainFile mainFile) {
    try {
      String filePath = sysMemoryDeviceService.selectSysMemoryDeviceById(memoryId).getSysMemoryDevice() +
          sysDataSetService.selectSysDataSetById(dataSetId).getDataSetDevice() + "\\main.in";
      mainFile.setMainFilePath(filePath);
      navigationService.creatMain(mainFile);
      return OUCAjaxResult.ok("main文件创建成功，路径为" + filePath);
    } catch (Exception e) {
      return OUCAjaxResult.error(e.toString());
    }
  }

  @ResponseBody
  @RequestMapping("/runMain")
  public OUCAjaxResult runMain(@RequestParam(value = "dataSetId", required = false, defaultValue = "11") Integer dataSetId,
                               @RequestParam(value = "memoryId", required = false, defaultValue = "14") Integer memoryId) {
    try {
      String mainDir = sysMemoryDeviceService.selectSysMemoryDeviceById(memoryId).getSysMemoryDevice() +
          sysDataSetService.selectSysDataSetById(dataSetId).getDataSetDevice();
      String runMain = navigationService.runMain(mainDir);
      if (runMain.contains("路径规划完成")) {
        return OUCAjaxResult.ok(runMain);
      } else {
        return OUCAjaxResult.error("error");
      }
    } catch (Exception e) {
      return OUCAjaxResult.error(e.toString());
    }
  }

  @ResponseBody
  @RequestMapping("/getMainOutFile")
  public OUCAjaxResult getMainOutFile(@RequestParam(value = "dataSetId", required = false, defaultValue = "11") Integer dataSetId,
                                      @RequestParam(value = "memoryId", required = false, defaultValue = "14") Integer memoryId,
                                      @RequestParam(value = "jobName") String jobName) {
    try {
      String fileDir = sysMemoryDeviceService.selectSysMemoryDeviceById(memoryId).getSysMemoryDevice() +
          sysDataSetService.selectSysDataSetById(dataSetId).getDataSetDevice();
      return OUCAjaxResult.ok(navigationService.getMainOutFile(fileDir, jobName));
    } catch (Exception e) {
      return OUCAjaxResult.error(e.toString());
    }
  }

  @ResponseBody
  @RequestMapping("/creatMapInFile")
  public OUCAjaxResult creatMapInFile(@RequestParam(value = "dataSetId", required = false, defaultValue = "11") Integer dataSetId,
                                      @RequestParam(value = "memoryId", required = false, defaultValue = "14") Integer memoryId,
                                      @RequestParam(value = "jobName") String jobName,
                                      @RequestParam(value = "limitVariable") float limitVariable) {
    try {
      String fileDir = sysMemoryDeviceService.selectSysMemoryDeviceById(memoryId).getSysMemoryDevice() +
          sysDataSetService.selectSysDataSetById(dataSetId).getDataSetDevice();
      MainOutFile mainOutFile = navigationService.getMainOutFile(fileDir, jobName);
      mainOutFile.setOutFileDir(fileDir);

      String pointSafetyDir = sysMemoryDeviceService.selectSysMemoryDeviceById(memoryId).getSysMemoryDevice() +
          sysDataSetService.selectSysDataSetById(dataSetId).getDataSetDevice() + File.separator + jobName + ".envout";
      Float[][] floats = navigationService.getEnvOutFile(pointSafetyDir).getDataArr().get(0);

      if (floats.length != mainOutFile.getNy() || floats[0].length != mainOutFile.getNx()) {
        return OUCAjaxResult.error("环境评估数据与环境map不匹配，请检查后重试");
      }
      String[][] outMap = mainOutFile.getOutMap();
      for (int i = 0; i < mainOutFile.getNy(); i++) {
        for (int j = 0; j < mainOutFile.getNx(); j++) {
          if ((!floats[i][j].isNaN()) && floats[i][j] > limitVariable) {
            outMap[i][j] = "1111";
          }
        }
      }
      mainOutFile.setOutMap(outMap);
      return OUCAjaxResult.ok(navigationService.creatMapInFile(mainOutFile));
    } catch (Exception e) {
      return OUCAjaxResult.error(e.toString());
    }
  }


  @ResponseBody
  @RequestMapping("/getMainOutFileToPng")
  public OUCAjaxResult getMainOutFileToPng(@RequestParam(value = "dataSetId", required = false, defaultValue = "11") Integer dataSetId,
                                           @RequestParam(value = "memoryId", required = false, defaultValue = "14") Integer memoryId,
                                           @RequestParam(value = "jobName") String jobName) {
    try {
      String fileDir = sysMemoryDeviceService.selectSysMemoryDeviceById(memoryId).getSysMemoryDevice() +
          sysDataSetService.selectSysDataSetById(dataSetId).getDataSetDevice();
      MainOutFile mainOutFile = navigationService.getMainOutFile(fileDir, jobName);
      return OUCAjaxResult.ok(navigationService.getPngFromMainOut(mainOutFile));
    } catch (Exception e) {
      return OUCAjaxResult.error(e.toString());
    }
  }

  @ResponseBody
  @RequestMapping("/pushMainOutFileToMysql")
  public OUCAjaxResult pushMainOutFileToMysql(@RequestParam(value = "dataSetId", required = false, defaultValue = "11") Integer dataSetId,
                                              @RequestParam(value = "memoryId", required = false, defaultValue = "14") Integer memoryId,
                                              @RequestParam(value = "historyRoteDataSetId", required = false, defaultValue = "15") Integer historyRoteDataSetId,
                                              @RequestParam(value = "historyRoteMemoryId", required = false, defaultValue = "18") Integer historyRoteMemoryId,
                                              @RequestParam(value = "jobName") String jobName,
                                              SysDataShipPath shipPath) {
    try {
      shipPath.setPathFilePath(File.separator + jobName);
      shipPath.setPathMemoryId(historyRoteMemoryId);
      shipPath.setPathDataSetId(historyRoteDataSetId);
      dataShipPathService.insertSysDataShipPath(shipPath);
      Integer pathId = dataShipPathService.selectNewInsert(null).getPathId();

      String outFilePath = sysMemoryDeviceService.selectSysMemoryDeviceById(memoryId).getSysMemoryDevice() +
          sysDataSetService.selectSysDataSetById(dataSetId).getDataSetDevice() + File.separator + jobName + ".out";
      String copyDir = sysMemoryDeviceService.selectSysMemoryDeviceById(historyRoteMemoryId).getSysMemoryDevice() +
          sysDataSetService.selectSysDataSetById(historyRoteDataSetId).getDataSetDevice() + File.separator + pathId;
      File file = new File(copyDir);
      if (!file.exists()) {
        file.mkdirs();
      }
      String copyFilePath = copyDir + File.separator + jobName + ".out";
      Files.copy(Paths.get(outFilePath), Paths.get(copyFilePath));
      String mapOutFilePath = sysMemoryDeviceService.selectSysMemoryDeviceById(memoryId).getSysMemoryDevice() +
          sysDataSetService.selectSysDataSetById(dataSetId).getDataSetDevice() + File.separator + jobName + "_map.out";
      String mapCopyFilePath = copyDir + File.separator + jobName + "_map.out";
      Files.copy(Paths.get(mapOutFilePath), Paths.get(mapCopyFilePath));
      SysDataShipPath dataShipPath = new SysDataShipPath();
      dataShipPath.setPathId(pathId);
      dataShipPath.setPathFilePath(File.separator + pathId + File.separator + jobName);
      dataShipPathService.updateSysDataShipPath(dataShipPath);
      return OUCAjaxResult.ok(pathId, "已保存" + jobName + ".out文件和map.out至数据库中");
    } catch (Exception e) {
      return OUCAjaxResult.error(e);
    }
  }

  @ResponseBody
  @RequestMapping("/getMainOutFileByMysql")
  public OUCAjaxResult getMainOutFileByMysql(@RequestParam(value = "shipPathId") Integer shipPathId) {
    try {
      SysDataShipPath sysDataShipPath = dataShipPathService.selectSysDataShipPathByPathId(shipPathId);
      if (sysDataShipPath == null || sysDataShipPath.getPathDataSetId() == null || sysDataShipPath.getPathFilePath() == null) {
        return OUCAjaxResult.error("未找到该航线信息");
      }
      String fileDir = sysMemoryDeviceService.selectSysMemoryDeviceById(sysDataShipPath.getPathMemoryId()).getSysMemoryDevice() +
          sysDataSetService.selectSysDataSetById(sysDataShipPath.getPathDataSetId()).getDataSetDevice();
      return OUCAjaxResult.ok(navigationService.getMainOutFile(fileDir, sysDataShipPath.getPathFilePath()));
    } catch (Exception e) {
      return OUCAjaxResult.error(e.toString());
    }
  }

  @ResponseBody
  @RequestMapping("/uploadFile")
  public OUCAjaxResult uploadFile(MultipartFile[] file,
                                  @RequestParam(value = "dataSetId", required = false, defaultValue = "11") Integer dataSetId,
                                  @RequestParam(value = "memoryId", required = false, defaultValue = "14") Integer memoryId) {
    try {


      for (MultipartFile multipartFile : file) {
        String fileDir = sysMemoryDeviceService.selectSysMemoryDeviceById(memoryId).getSysMemoryDevice() +
            sysDataSetService.selectSysDataSetById(dataSetId).getDataSetDevice();
        String fileName = FileUtils.getName(multipartFile.getOriginalFilename());
        multipartFile.transferTo(new File(fileDir, fileName));
      }
      return OUCAjaxResult.ok("文件成功上传");
    } catch (Exception e) {
      return OUCAjaxResult.error("未成功上传");
    }
  }

  @ResponseBody
  @RequestMapping("/getFileList")
  public OUCAjaxResult getFileList(@RequestParam(value = "dataSetId", required = false, defaultValue = "11") Integer dataSetId,
                                   @RequestParam(value = "memoryId", required = false, defaultValue = "14") Integer memoryId,
                                   @RequestParam(value = "fileExportName", required = false, defaultValue = ".nc") String fileExportName,
                                   @RequestParam(value = "time", required = false) String time) {
    try {
      String fileDir = sysMemoryDeviceService.selectSysMemoryDeviceById(memoryId).getSysMemoryDevice() +
          sysDataSetService.selectSysDataSetById(dataSetId).getDataSetDevice();
      List<String> fileList = FileUtils.getFileList(fileDir);
      List<String> fileResult = new ArrayList<>();
      for (String filePath : fileList) {
        String name = FileUtils.getName(filePath);
        if (name.endsWith(fileExportName)) {
          if (time == null || name.startsWith(time)) {
            fileResult.add(name);
          }
        }
      }
      return OUCAjaxResult.ok(fileResult);
    } catch (Exception e) {
      return OUCAjaxResult.error("文件地址有误，请重试");
    }
  }

  @ResponseBody
  @RequestMapping("/creatSpectrumSetting")
  public OUCAjaxResult creatSpectrumSetting(@RequestParam(value = "dataSetId", required = false, defaultValue = "12") Integer dataSetId,
                                            @RequestParam(value = "memoryId", required = false, defaultValue = "15") Integer memoryId,
                                            SpectrumSetting spectrumSetting) {
    try {
      String fileDir = sysMemoryDeviceService.selectSysMemoryDeviceById(memoryId).getSysMemoryDevice() +
          sysDataSetService.selectSysDataSetById(dataSetId).getDataSetDevice() + "\\Setting.txt";
      spectrumSetting.setFileDir(fileDir);
      navigationService.creatSpectrumSetting(spectrumSetting);
      return OUCAjaxResult.ok("spectrumSetting文件创建成功，路径为" + fileDir);
    } catch (Exception e) {
      return OUCAjaxResult.error(e.toString());
    }
  }

  @ResponseBody
  @RequestMapping("/getRaoFileList")
  public OUCAjaxResult getRaoFileList(@RequestParam(value = "dataSetId", required = false, defaultValue = "12") Integer dataSetId,
                                      @RequestParam(value = "memoryId", required = false, defaultValue = "15") Integer memoryId,
                                      @RequestParam(value = "fileExportName", required = false, defaultValue = ".txt") String fileExportName) {
    try {
      String fileDir = sysMemoryDeviceService.selectSysMemoryDeviceById(memoryId).getSysMemoryDevice() +
          sysDataSetService.selectSysDataSetById(dataSetId).getDataSetDevice();
      List<String> fileList = FileUtils.getFileList(fileDir);
      List<String> fileResult = new ArrayList<>();
      for (String filePath : fileList) {
        String name = FileUtils.getName(filePath);
        if (name.endsWith(fileExportName) && (!(name.startsWith("result") || name.startsWith("Setting")))) {
          fileResult.add(name);
        }
      }
      return OUCAjaxResult.ok(fileResult);
    } catch (Exception e) {
      return OUCAjaxResult.error("未成功上传");
    }
  }

  @ResponseBody
  @RequestMapping("getRaoFileByShip")
  public OUCAjaxResult getRaoFileByShip(@RequestParam(value = "shipId") Integer shipId,
                                        @RequestParam(value = "raoDataSetId", required = false, defaultValue = "8") Integer raoDataSetId,
                                        @RequestParam(value = "raoMemoryId", required = false, defaultValue = "11") Integer raoMemoryId,
                                        @RequestParam(value = "dataSetId", required = false, defaultValue = "12") Integer dataSetId,
                                        @RequestParam(value = "memoryId", required = false, defaultValue = "15") Integer memoryId) {
    try {
      String raoFileDir = sysMemoryDeviceService.selectSysMemoryDeviceById(raoMemoryId).getSysMemoryDevice() +
          sysDataSetService.selectSysDataSetById(raoDataSetId).getDataSetDevice();
      String spectrumDileDir = sysMemoryDeviceService.selectSysMemoryDeviceById(memoryId).getSysMemoryDevice() +
          sysDataSetService.selectSysDataSetById(dataSetId).getDataSetDevice();
      SysDataShip dataShip = dataShipService.selectSysDataShipById(shipId);
      if (dataShip == null) {
        return OUCAjaxResult.error("未找到该舰船信息");
      }
      String shipRaoPath = dataShip.getShipRaoPath();
      if (shipRaoPath == null) {
        return OUCAjaxResult.error("该舰船未上传RAO文件");
      }
      Files.copy(Paths.get(raoFileDir + shipRaoPath), Paths.get(spectrumDileDir + File.separator + FileUtils.getName(shipRaoPath)), StandardCopyOption.REPLACE_EXISTING);
      return OUCAjaxResult.ok("rao文件获取成功，名称为：" + FileUtils.getName(shipRaoPath));
    } catch (Exception e) {
      return OUCAjaxResult.error(e);
    }
  }

  @ResponseBody
  @RequestMapping("/runSpectrum")
  public OUCAjaxResult runSpectrum(@RequestParam(value = "dataSetId", required = false, defaultValue = "12") Integer dataSetId,
                                   @RequestParam(value = "memoryId", required = false, defaultValue = "15") Integer memoryId) {
    try {
      String spectrumDir = sysMemoryDeviceService.selectSysMemoryDeviceById(memoryId).getSysMemoryDevice() +
          sysDataSetService.selectSysDataSetById(dataSetId).getDataSetDevice();
      String runSpectrum = navigationService.runSpectrum(spectrumDir);
      if (runSpectrum.contains("Successfully wrote")) {
        return OUCAjaxResult.ok();
      } else {
        return OUCAjaxResult.error("error");
      }
    } catch (Exception e) {
      return OUCAjaxResult.error(e.toString());
    }
  }

  @ResponseBody
  @RequestMapping("/getSpectrumResult")
  public OUCAjaxResult getSpectrumResult(@RequestParam(value = "dataSetId", required = false, defaultValue = "12") Integer dataSetId,
                                         @RequestParam(value = "memoryId", required = false, defaultValue = "15") Integer memoryId) {
    try {
      String spectrumDir = sysMemoryDeviceService.selectSysMemoryDeviceById(memoryId).getSysMemoryDevice() +
          sysDataSetService.selectSysDataSetById(dataSetId).getDataSetDevice();
      return OUCAjaxResult.ok(navigationService.getSpectrumResult(spectrumDir));
    } catch (Exception e) {
      return OUCAjaxResult.error(e.toString());
    }
  }

  @ResponseBody
  @RequestMapping("/creatPointSafety")
  public OUCAjaxResult creatPointSafety(@RequestParam(value = "dataSetId", required = false, defaultValue = "13") Integer dataSetId,
                                        @RequestParam(value = "memoryId", required = false, defaultValue = "16") Integer memoryId,
                                        PointSafety pointSafety) {
    try {
      String fileDir = sysMemoryDeviceService.selectSysMemoryDeviceById(memoryId).getSysMemoryDevice() +
          sysDataSetService.selectSysDataSetById(dataSetId).getDataSetDevice() + File.separator + "ProjectName_Time.in";
      pointSafety.setFileDir(fileDir);
      navigationService.creatPointSafety(pointSafety);
      return OUCAjaxResult.ok("ProjectName_Time.in文件创建成功，路径为" + fileDir);
    } catch (Exception e) {
      return OUCAjaxResult.error(e.toString());
    }
  }

  @ResponseBody
  @RequestMapping("/runPointSafety")
  public OUCAjaxResult runPointSafety(@RequestParam(value = "dataSetId", required = false, defaultValue = "13") Integer dataSetId,
                                      @RequestParam(value = "memoryId", required = false, defaultValue = "16") Integer memoryId) {
    try {
      String pointSafetyDir = sysMemoryDeviceService.selectSysMemoryDeviceById(memoryId).getSysMemoryDevice() +
          sysDataSetService.selectSysDataSetById(dataSetId).getDataSetDevice();
      return OUCAjaxResult.ok(navigationService.runPointSafety(pointSafetyDir));
    } catch (Exception e) {
      return OUCAjaxResult.error(e.toString());
    }
  }

  @ResponseBody
  @RequestMapping("/getPointSafety")
  public OUCAjaxResult getPointSafety(@RequestParam(value = "dataSetId", required = false, defaultValue = "13") Integer dataSetId,
                                      @RequestParam(value = "memoryId", required = false, defaultValue = "16") Integer memoryId,
                                      @RequestParam(value = "projectName") String projectName,
                                      @RequestParam(value = "projectTime") String projectTime) {
    try {
      String fileDir = sysMemoryDeviceService.selectSysMemoryDeviceById(memoryId).getSysMemoryDevice() +
          sysDataSetService.selectSysDataSetById(dataSetId).getDataSetDevice() + File.separator;
      return OUCAjaxResult.ok(navigationService.getPointSafety(fileDir, projectName, projectTime));
    } catch (Exception e) {
      return OUCAjaxResult.error("未找到该运行结果");
    }
  }

  @ResponseBody
  @RequestMapping("/getRoteInFileList")
  public OUCAjaxResult getRoteInFileList(@RequestParam(value = "dataSetId", required = false, defaultValue = "14") Integer dataSetId,
                                         @RequestParam(value = "memoryId", required = false, defaultValue = "17") Integer memoryId,
                                         @RequestParam(value = "fileExportName", required = false, defaultValue = ".out") String fileExportName) {
    try {
      String fileDir = sysMemoryDeviceService.selectSysMemoryDeviceById(memoryId).getSysMemoryDevice() +
          sysDataSetService.selectSysDataSetById(dataSetId).getDataSetDevice();
      List<String> fileList = FileUtils.getFileList(fileDir);
      List<String> fileResult = new ArrayList<>();
      for (String filePath : fileList) {
        String name = FileUtils.getName(filePath);
        if (name.endsWith(fileExportName) && (!name.contains("_"))) {
          fileResult.add(name);
        }
      }
      return OUCAjaxResult.ok(fileResult);
    } catch (Exception e) {
      return OUCAjaxResult.error("未查找到工作文件夹");
    }
  }

  @ResponseBody
  @RequestMapping("/uploadRoteInFile")
  public OUCAjaxResult uploadRoteInFile(MultipartFile[] file,
                                        @RequestParam(value = "dataSetId", required = false, defaultValue = "14") Integer dataSetId,
                                        @RequestParam(value = "memoryId", required = false, defaultValue = "17") Integer memoryId) {
    try {
      for (MultipartFile multipartFile : file) {
        String fileDir = sysMemoryDeviceService.selectSysMemoryDeviceById(memoryId).getSysMemoryDevice() +
            sysDataSetService.selectSysDataSetById(dataSetId).getDataSetDevice();
        String fileName = FileUtils.getName(multipartFile.getOriginalFilename());
        multipartFile.transferTo(new File(fileDir, fileName));
      }
      return OUCAjaxResult.ok("文件成功上传");
    } catch (Exception e) {
      return OUCAjaxResult.error("未成功上传");
    }
  }

  @ResponseBody
  @RequestMapping("/getRoteOutFileFromMysql")
  public OUCAjaxResult getRoteOutFileFromMysql(@RequestParam(value = "dataSetId", required = false, defaultValue = "14") Integer dataSetId,
                                               @RequestParam(value = "memoryId", required = false, defaultValue = "17") Integer memoryId,
                                               @RequestParam(value = "shipPathId") Integer shipPathId) {
    try {
      SysDataShipPath sysDataShipPath = dataShipPathService.selectSysDataShipPathByPathId(shipPathId);
      if (sysDataShipPath == null || sysDataShipPath.getPathFilePath() == null) {
        return OUCAjaxResult.error("未查询到该航线信息");
      }
      String outFilePath = sysMemoryDeviceService.selectSysMemoryDeviceById(sysDataShipPath.getPathMemoryId()).getSysMemoryDevice() +
          sysDataSetService.selectSysDataSetById(sysDataShipPath.getPathDataSetId()).getDataSetDevice() + sysDataShipPath.getPathFilePath() + ".out";
      String jobFilePath = sysMemoryDeviceService.selectSysMemoryDeviceById(memoryId).getSysMemoryDevice() +
          sysDataSetService.selectSysDataSetById(dataSetId).getDataSetDevice() + File.separator + FileUtils.getName(sysDataShipPath.getPathFilePath()) + ".out";
      Files.copy(Paths.get(outFilePath), Paths.get(jobFilePath), StandardCopyOption.REPLACE_EXISTING);
      return OUCAjaxResult.ok(FileUtils.getName(sysDataShipPath.getPathFilePath()));
    } catch (Exception e) {
      return OUCAjaxResult.error(e);
    }
  }

  @ResponseBody
  @RequestMapping("/creatRoteSafety")
  public OUCAjaxResult creatRoteSafety(@RequestParam(value = "dataSetId", required = false, defaultValue = "14") Integer dataSetId,
                                       @RequestParam(value = "memoryId", required = false, defaultValue = "17") Integer memoryId,
                                       RoteSafety roteSafety) {
    try {
      String filePath = sysMemoryDeviceService.selectSysMemoryDeviceById(memoryId).getSysMemoryDevice() +
          sysDataSetService.selectSysDataSetById(dataSetId).getDataSetDevice() + File.separator + "ShipParameters.in";
      roteSafety.setFileDir(filePath);
      navigationService.creatRoteSafety(roteSafety);
      return OUCAjaxResult.ok("ShipParameters.in文件创建成功，路径为" + filePath);
    } catch (Exception e) {
      return OUCAjaxResult.error(e.toString());
    }
  }

  @ResponseBody
  @RequestMapping("/runRoteSafety")
  public OUCAjaxResult runRoteSafety(@RequestParam(value = "dataSetId", required = false, defaultValue = "14") Integer dataSetId,
                                     @RequestParam(value = "memoryId", required = false, defaultValue = "17") Integer memoryId) {
    try {
      String roteSafetyDir = sysMemoryDeviceService.selectSysMemoryDeviceById(memoryId).getSysMemoryDevice() +
          sysDataSetService.selectSysDataSetById(dataSetId).getDataSetDevice();
      String runRoteSafety = navigationService.runRoteSafety(roteSafetyDir);
      if (runRoteSafety.contains("结果已保存至")) {
        return OUCAjaxResult.ok();
      } else {
        return OUCAjaxResult.error("error");
      }
    } catch (Exception e) {
      return OUCAjaxResult.error(e.toString());
    }
  }

  @ResponseBody
  @RequestMapping("/getRoteSafetyOutFile")
  public OUCAjaxResult getRoteSafetyOutFile(@RequestParam(value = "dataSetId", required = false, defaultValue = "14") Integer dataSetId,
                                            @RequestParam(value = "memoryId", required = false, defaultValue = "17") Integer memoryId,
                                            @RequestParam(value = "projectName") String projectName,
                                            @RequestParam(value = "projectTime", required = false, defaultValue = "") String projectTime) {
    try {
      String fileDir = sysMemoryDeviceService.selectSysMemoryDeviceById(memoryId).getSysMemoryDevice() +
          sysDataSetService.selectSysDataSetById(dataSetId).getDataSetDevice();
      return OUCAjaxResult.ok(navigationService.getRoteSafety(fileDir, projectName, projectTime));
    } catch (Exception e) {
      return OUCAjaxResult.error(e.toString());
    }
  }

  @ResponseBody
  @RequestMapping("/getRoteToSpectrum")
  public OUCAjaxResult getRoteToSpectrum(@RequestParam(value = "shipPathId") Integer shipPathId,
                                         @RequestParam(value = "dataSetId", required = false, defaultValue = "12") Integer dataSetId,
                                         @RequestParam(value = "memoryId", required = false, defaultValue = "15") Integer memoryId,
                                         @RequestParam(value = "shipSpeed", required = false, defaultValue = "10.0") Double shipSpeed,
                                         SpectrumSetting spectrumSetting) {
    try {
      SysDataShipPath sysDataShipPath = dataShipPathService.selectSysDataShipPathByPathId(shipPathId);
      if (sysDataShipPath == null || sysDataShipPath.getPathDataSetId() == null || sysDataShipPath.getPathFilePath() == null) {
        return OUCAjaxResult.error("未找到该航线信息");
      }
      String fileDir = sysMemoryDeviceService.selectSysMemoryDeviceById(sysDataShipPath.getPathMemoryId()).getSysMemoryDevice() +
          sysDataSetService.selectSysDataSetById(sysDataShipPath.getPathDataSetId()).getDataSetDevice() + File.separator + shipPathId;
      MainOutFile mainOutFile = navigationService.getMainOutFile(fileDir, FileUtils.getName(sysDataShipPath.getPathFilePath()));
      if (mainOutFile == null) {
        return OUCAjaxResult.error("未找到航线文件");
      }
      String spFileDir = sysMemoryDeviceService.selectSysMemoryDeviceById(memoryId).getSysMemoryDevice() +
          sysDataSetService.selectSysDataSetById(dataSetId).getDataSetDevice();
      spectrumSetting.setFileDir(spFileDir + "\\Setting.txt");
      Integer settingNumber = navigationService.creatSpectrumSettingByRote(mainOutFile, spectrumSetting, shipSpeed);
      String runSpectrum = navigationService.runSpectrum(spFileDir);
      List<SpectrumResult> spectrumResult = navigationService.getSpectrumResult(spFileDir);
      if (settingNumber != spectrumResult.size() || (!runSpectrum.contains("Successfully wrote"))) {
        return OUCAjaxResult.error("航线分析计算有误");
      }
      int t = 0;
      for (ShipPathAll shipPathAll : mainOutFile.getShipPathAllList()) {
        for (ShipPathList shipPathList : shipPathAll.getShipPathLists()) {
          for (ShipPath shipPath : shipPathList.getShipPathList()) {
            shipPath.setSpectrumResult(spectrumResult.get(t++));
          }
        }
      }
      return OUCAjaxResult.ok(mainOutFile);
    } catch (Exception e) {
      return OUCAjaxResult.error(e.toString());
    }
  }

  @ResponseBody
  @RequestMapping("/getWaToSpectrum")
  public OUCAjaxResult getWaToSpectrum(@RequestParam(value = "preDataSetId", required = false, defaultValue = "11") Integer preDataSetId,
                                       @RequestParam(value = "preMemoryId", required = false, defaultValue = "14") Integer preMemoryId,
                                       @RequestParam(value = "preJobName") String preJobName,
                                       @RequestParam(value = "spDataSetId", required = false, defaultValue = "12") Integer spDataSetId,
                                       @RequestParam(value = "spMemoryId", required = false, defaultValue = "15") Integer spMemoryId,
                                       @RequestParam(value = "shipSpeed", required = false, defaultValue = "10.0") Double shipSpeed,
                                       SpectrumSetting spectrumSetting) {
    try {
      PreFile preFile = new PreFile();
      if (preJobName.contains(".wa")) {
        preJobName = FileUtils.getFileNameNoEx(preJobName);
      }
      preFile.setJobName(preJobName);
      preFile.setPreFilePath(sysMemoryDeviceService.selectSysMemoryDeviceById(preMemoryId).getSysMemoryDevice() +
          sysDataSetService.selectSysDataSetById(preDataSetId).getDataSetDevice());
      PreFile preWaFile = navigationService.getPreWaFile(preFile);
      String spFileDir = sysMemoryDeviceService.selectSysMemoryDeviceById(spMemoryId).getSysMemoryDevice() +
          sysDataSetService.selectSysDataSetById(spDataSetId).getDataSetDevice();
      spectrumSetting.setFileDir(spFileDir + "\\Setting.txt");
      Integer settingNumber = navigationService.creatSpectrumSettingByWa(preWaFile, spectrumSetting, shipSpeed);
      String runSpectrum = navigationService.runSpectrum(spFileDir);
      List<SpectrumResult> spectrumResult = navigationService.getSpectrumResult(spFileDir);
      if (settingNumber != spectrumResult.size() || (!runSpectrum.contains("Successfully wrote"))) {
        return OUCAjaxResult.error("航线分析计算有误");
      }
      int t = 0;
      List<List<SpectrumResult>> result = new ArrayList<>();
      for (int i = 0; i < preWaFile.getNy(); i++) {
        List<SpectrumResult> list = new ArrayList<>();
        for (int j = 0; j < preWaFile.getNx(); j++) {
          list.add(spectrumResult.get(t++));
        }
        result.add(list);
      }
      return OUCAjaxResult.ok(result);
    } catch (Exception e) {
      return OUCAjaxResult.error(e.toString());
    }
  }

  @ResponseBody
  @RequestMapping("/getWaToSpectrumDemo")
  public OUCAjaxResult getWaToSpectrumDemo(@RequestParam(value = "spDataSetId", required = false, defaultValue = "12") Integer spDataSetId,
                                           @RequestParam(value = "spMemoryId", required = false, defaultValue = "15") Integer spMemoryId,
                                           @RequestParam(value = "nx", required = false, defaultValue = "258") Integer nx,
                                           @RequestParam(value = "ny", required = false, defaultValue = "170") Integer ny,
                                           @RequestParam(value = "resultFileName", required = false, defaultValue = "results258170") String resultFileName) {
    try {
      String spFileDir = sysMemoryDeviceService.selectSysMemoryDeviceById(spMemoryId).getSysMemoryDevice() +
          sysDataSetService.selectSysDataSetById(spDataSetId).getDataSetDevice();
      List<SpectrumResult> spectrumResult = navigationService.getSpectrumResultDemo(spFileDir, resultFileName);
      if (nx * ny != spectrumResult.size()) {
        return OUCAjaxResult.error("航线分析计算有误");
      }
      int t = 0;
      List<List<SpectrumResult>> result = new ArrayList<>();
      for (int i = 0; i < ny; i++) {
        List<SpectrumResult> list = new ArrayList<>();
        for (int j = 0; j < nx; j++) {
          list.add(spectrumResult.get(t++));
        }
        result.add(list);
      }
      return OUCAjaxResult.ok(result);
    } catch (Exception e) {
      return OUCAjaxResult.error(e.toString());
    }
  }

  @ResponseBody
  @RequestMapping("/creatShipParameters")
  public OUCAjaxResult creatShipParameters(@RequestParam(value = "dataSetId", required = false, defaultValue = "11") Integer dataSetId,
                                           @RequestParam(value = "memoryId", required = false, defaultValue = "14") Integer memoryId,
                                           RoteSafety roteSafety) {
    try {
      String filePath = sysMemoryDeviceService.selectSysMemoryDeviceById(memoryId).getSysMemoryDevice() +
          sysDataSetService.selectSysDataSetById(dataSetId).getDataSetDevice() + File.separator + "ShipParameters.in";
      roteSafety.setFileDir(filePath);
      if (roteSafety.getProjectName().contains(".")) {
        roteSafety.setProjectName(FileUtils.getFileNameNoEx(roteSafety.getProjectName()));
      }
      navigationService.creatShipParameters(roteSafety);
      return OUCAjaxResult.ok("ShipParameters.in文件创建成功，路径为" + filePath);
    } catch (Exception e) {
      return OUCAjaxResult.error(e.toString());
    }
  }

  @ResponseBody
  @RequestMapping("/runSafetyEnv")
  public OUCAjaxResult runSafetyEnv(@RequestParam(value = "dataSetId", required = false, defaultValue = "11") Integer dataSetId,
                                    @RequestParam(value = "memoryId", required = false, defaultValue = "14") Integer memoryId) {
    try {
      String pointSafetyDir = sysMemoryDeviceService.selectSysMemoryDeviceById(memoryId).getSysMemoryDevice() +
          sysDataSetService.selectSysDataSetById(dataSetId).getDataSetDevice();
      String runSafetyEnv = navigationService.runSafetyEnv(pointSafetyDir);
      if (runSafetyEnv.contains("结果已保存至")) {
        return OUCAjaxResult.ok(runSafetyEnv);
      } else {
        return OUCAjaxResult.error("error");
      }
    } catch (Exception e) {
      return OUCAjaxResult.error(e.toString());
    }
  }

  @ResponseBody
  @RequestMapping("/getEnvOutFile")
  public OUCAjaxResult getEnvOutFile(@RequestParam(value = "dataSetId", required = false, defaultValue = "11") Integer dataSetId,
                                     @RequestParam(value = "memoryId", required = false, defaultValue = "14") Integer memoryId,
                                     @RequestParam(value = "projectName") String projectName) {
    try {
      String pointSafetyDir = sysMemoryDeviceService.selectSysMemoryDeviceById(memoryId).getSysMemoryDevice() +
          sysDataSetService.selectSysDataSetById(dataSetId).getDataSetDevice() + File.separator + projectName + ".envout";
      return OUCAjaxResult.ok(navigationService.getEnvOutFile(pointSafetyDir));
    } catch (Exception e) {
      return OUCAjaxResult.error(e.toString());
    }
  }

  @ResponseBody
  @RequestMapping("/getEnvOutToPng")
  public OUCAjaxResult getEnvOutToPng(@RequestParam(value = "dataSetId", required = false, defaultValue = "11") Integer dataSetId,
                                      @RequestParam(value = "memoryId", required = false, defaultValue = "14") Integer memoryId,
                                      @RequestParam(value = "projectName") String projectName) {
    try {
      String pointSafetyDir = sysMemoryDeviceService.selectSysMemoryDeviceById(memoryId).getSysMemoryDevice() +
          sysDataSetService.selectSysDataSetById(dataSetId).getDataSetDevice() + File.separator + projectName + ".envout";
      PreFile envOutFile = navigationService.getEnvOutFile(pointSafetyDir);
      List<byte[]> result = new ArrayList<>();
      result.add(navigationService.getPngFromData2D(envOutFile.getDataArr().get(0)));
      result.add(navigationService.getPngFromData2D(envOutFile.getDataArr().get(1)));
      result.add(navigationService.getPngFromData2D(envOutFile.getDataArr().get(2)));
      result.add(navigationService.getPngFromData2D(envOutFile.getDataArr().get(3)));
      return OUCAjaxResult.ok(result);
    } catch (Exception e) {
      return OUCAjaxResult.error(e.toString());
    }
  }

  @ResponseBody
  @RequestMapping("/getEnvSpPng")
  public OUCAjaxResult getEnvSpPng(@RequestParam(value = "spDataSetId", required = false, defaultValue = "12") Integer spDataSetId,
                                   @RequestParam(value = "spMemoryId", required = false, defaultValue = "15") Integer spMemoryId,
                                   @RequestParam(value = "nx", required = false, defaultValue = "258") Integer nx,
                                   @RequestParam(value = "ny", required = false, defaultValue = "170") Integer ny,
                                   @RequestParam(value = "resultFileName", required = false, defaultValue = "results") String resultFileName) {
    try {
      String spFileDir = sysMemoryDeviceService.selectSysMemoryDeviceById(spMemoryId).getSysMemoryDevice() +
          sysDataSetService.selectSysDataSetById(spDataSetId).getDataSetDevice();
      List<SpectrumResult> spectrumResult = navigationService.getSpectrumResultDemo(spFileDir, resultFileName);
      if (nx * ny != spectrumResult.size()) {
        return OUCAjaxResult.error("航线分析计算有误");
      }
      int t = 0;
      Float[][] wiArr = new Float[ny][nx];
      Float[][] siArr = new Float[ny][nx];
      Float[][] peArr = new Float[ny][nx];
      for (int i = 0; i < ny; i++) {
        for (int j = 0; j < nx; j++) {
          wiArr[i][j] = spectrumResult.get(t).getWi().floatValue();
          siArr[i][j] = spectrumResult.get(t).getSi().floatValue();
          peArr[i][j] = spectrumResult.get(t).getPe().floatValue();
          t++;
        }
      }
      List<byte[]> result = new ArrayList<>();
      result.add(navigationService.getPngFromData2D(wiArr));
      result.add(navigationService.getPngFromData2D(siArr));
      result.add(navigationService.getPngFromData2D(peArr));
      return OUCAjaxResult.ok(result);
    } catch (Exception e) {
      return OUCAjaxResult.error(e.toString());
    }
  }

  
}